var wxpay = require('../../utils/pay.js')
const WXAPI = require('apifm-wxapi')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: undefined,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let recharge_amount_min = wx.getStorageSync('recharge_amount_min')
    if (!recharge_amount_min) {
      recharge_amount_min = 0;
    }
    this.setData({
      uid: wx.getStorageSync('uid'),
      recharge_amount_min: recharge_amount_min
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  // 用户余额充值
  async bindSave(e) {
    var that = this;
    var amount = e.detail.value.amount;

    if (amount == "" || amount * 1 <= 0) {
      wx.showModal({
        title: '错误',
        content: '请填写正确的充值金额',
        showCancel: false
      })
      return
    }
    if (amount * 1 < 1) {
      wx.showModal({
        title: '错误',
        content: '单次充值金额至少' + 1 + '元',
        showCancel: false
      })
      return
    }
    if (amount * 1 > 5000) {
      wx.showModal({
        title: '错误',
        content: '单次充值金额最多5000元',
        showCancel: false
      })
      return
    }
    var rechargeData = {'rechargeAmount':amount,'acount':wx.getStorageSync('uid')}
    console.log("充值信息："+JSON.stringify(rechargeData))
    const res = await WXAPI.userRecharge(rechargeData);
    console.log("充值结果："+JSON.stringify(res))
    if(res.code == 20000){
      wx.showModal({
        title: '成功',
        content: '充值成功，充值金额' + amount + '元',
        showCancel: false
      })
    }else{
      wx.showModal({
        title: '充值失败',
        content: res.msg,
        showCancel: false
      })
    }
  }
})